package com.example.testcoroutine

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope

class MActivity0 : AppCompatActivity() {


    lateinit var tv: TextView
    lateinit var runnable: Runnable
    lateinit var handler: Handler

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        tv = findViewById(R.id.tv)
        handler = Handler(Looper.getMainLooper())

        runnable = object : Runnable {
            var count = 0
            override fun run() {
                count++
                tv.text = "$count running..."
                handler.postDelayed(this, 1500)
            }
        }
        lifecycleScope.launchWhenResumed {
            handler.post(runnable)
        }
    }

    override fun onPause() {
        handler.removeCallbacks(runnable)
        println("MActivity onPause")
        super.onPause()
    }
}